package com.das.mechanic_base.adapter.alone;

import android.content.Context;
import android.graphics.Color;
import android.view.HapticFeedbackConstants;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.alone.AloneNewBean;
import com.das.mechanic_base.def.alone.AloneSubmitStatus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chensi on 2018/5/3.
 */

public class X3AloneChoiceAdapter extends BaseAdapter {

    private List<AloneNewBean.ItemQuestionListBean.QuestionDetectionTempletQuestionListBean.QuestionCarMilesDeductionEntityListBean> dataList;

    private Context context;

    private List<Boolean> isCheck;

    /**
     * 可能是本地缓存的数据 也可能是后台返回的 已经提交过的数据
     */
    private String content;

    public X3AloneChoiceAdapter(List<AloneNewBean.ItemQuestionListBean.QuestionDetectionTempletQuestionListBean.QuestionCarMilesDeductionEntityListBean> dataList, Context context, String content) {
        this.dataList = dataList;
        this.context = context;
        this.content = content;
        isCheck = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            isCheck.add(content.equals(dataList.get(i).optionDescription));
        }
    }

    /**
     * 刷线选中的项目
     * @param content
     */
    public void changeSelectChoice(String content) {
        this.content = content;
        isCheck.clear();
        for (int i = 0; i < dataList.size(); i++) {
            isCheck.add(content.equals(dataList.get(i).optionDescription));
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if (view == null) {
            view = View.inflate(context, R.layout.x3_alone_lv_item,null);
            viewHolder = new ViewHolder();
            viewHolder.tv_content = view.findViewById(R.id.tv_content);
            viewHolder.tv_title = view.findViewById(R.id.tv_title);
            viewHolder.rl_main = view.findViewById(R.id.rl_main);
            viewHolder.tv_status = view.findViewById(R.id.tv_status);
            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.tv_title.setText(dataList.get(i).optionSingle + ".");
        viewHolder.tv_content.setText(dataList.get(i).optionDescription + "");

        //选中的时候
        if (isCheck.get(i)) {
            viewHolder.tv_status.setVisibility(View.VISIBLE);
            viewHolder.rl_main.setBackgroundResource(R.drawable.x3_alone_btn_select);

            if (AloneSubmitStatus.MINOR.equals(dataList.get(i).status)) {

                viewHolder.rl_main.setBackgroundResource(R.drawable.x3_alone_choice_minor_bg);
                viewHolder.tv_content.setTextColor(Color.parseColor("#F3850F"));
                viewHolder.tv_title.setTextColor(Color.parseColor("#F3850F"));
                viewHolder.tv_status.setTextColor(Color.parseColor("#F3850F"));
                viewHolder.tv_status.setText("(" + context.getString(R.string.x3_minor_notice) + ")");
            }else if (AloneSubmitStatus.MAJOR.equals(dataList.get(i).status)){

                viewHolder.rl_main.setBackgroundResource(R.drawable.x3_alone_choice_dangerous_bg);
                viewHolder.tv_content.setTextColor(Color.parseColor("#F64458"));
                viewHolder.tv_title.setTextColor(Color.parseColor("#F64458"));
                viewHolder.tv_status.setTextColor(Color.parseColor("#F64458"));
                viewHolder.tv_status.setText("(" + context.getString(R.string.x3_major_notice) + ")");
            }else if (AloneSubmitStatus.DANGEROUS.equals(dataList.get(i).status)) {

                viewHolder.rl_main.setBackgroundResource(R.drawable.x3_alone_choice_dangerous_bg);
                viewHolder.tv_content.setTextColor(Color.parseColor("#F64458"));
                viewHolder.tv_title.setTextColor(Color.parseColor("#F64458"));
                viewHolder.tv_status.setTextColor(Color.parseColor("#F64458"));
                viewHolder.tv_status.setText("(" + context.getString(R.string.x3_error_notice) + ")");
            }else {

                viewHolder.rl_main.setBackgroundResource(R.drawable.x3_alone_choice_normal_bg);
                viewHolder.tv_content.setTextColor(Color.parseColor("#18BE6B"));
                viewHolder.tv_title.setTextColor(Color.parseColor("#18BE6B"));
                viewHolder.tv_status.setTextColor(Color.parseColor("#18BE6B"));
                viewHolder.tv_status.setText("(" + context.getString(R.string.x3_normal_notice) + ")");
            }

        }else {
            viewHolder.tv_status.setVisibility(View.GONE);
            viewHolder.rl_main.setBackgroundResource(R.drawable.x3_alone_choice_no_select_bg2);
            viewHolder.tv_content.setTextColor(ContextCompat.getColor(context,R.color.c333));
            viewHolder.tv_title.setTextColor(ContextCompat.getColor(context,R.color.c333));
        }

        View finalView = view;
        view.setOnClickListener(v -> {
            isCheck.clear();
            finalView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
            for (int j = 0; j < dataList.size(); j++) {
                if (j == i) {
                    isCheck.add(true);
                }else {
                    isCheck.add(false);
                }
            }
            if (iOnClickAdapter != null) {
                content = getSaveCheckData();
                iOnClickAdapter.showAdapterData(content);
            }

            notifyDataSetChanged();
        });

        return view;
    }

    public String getSaveCheckData (){
        String content = null;
        for (int j = 0; j < isCheck.size(); j ++) {
            if (isCheck.get(j)) {
                content = dataList.get(j).optionDescription;
                break;
            }
        }

        return content;
    }

    IOnClickAdapter iOnClickAdapter;

    public void setiOnClickAdapter(IOnClickAdapter iOnClickAdapter) {
        this.iOnClickAdapter = iOnClickAdapter;
    }

    public interface IOnClickAdapter {
        void showAdapterData(String content);
    }

    class ViewHolder {
        TextView tv_status;
        TextView tv_title;
        TextView tv_content;
        LinearLayout rl_main;
    }
}

